package org.lex.dbq.model.export;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.lex.io.utils.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class Exporter {
	protected Logger log = LoggerFactory.getLogger(getClass());

	public void export(ResultSet rs, String path, String encoding) throws FileNotFoundException, UnsupportedEncodingException, IOException,
			SQLException {
		FileOutputStream fos = FileUtils.openFile(new File(path));
		OutputStreamWriter osw = new OutputStreamWriter(fos, encoding);
		this.export(rs, osw);
	}

	public void export(ResultSet rs, File out, String encoding) throws FileNotFoundException, UnsupportedEncodingException, IOException, SQLException {
		FileOutputStream fos = FileUtils.openFile(out);
		OutputStreamWriter osw = new OutputStreamWriter(fos, encoding);
		this.export(rs, osw);
	}

	public void export(ResultSet rs, OutputStream out, String encoding) throws UnsupportedEncodingException, IOException, SQLException {
		OutputStreamWriter writer = new OutputStreamWriter(out, encoding);
		this.export(rs, writer);
	}

	public abstract void export(ResultSet rs, Writer out) throws IOException, SQLException;
}
